"""
connect to database
"""


from connect_data_src import ConnectDataSource


class MysqlOperation(ConnectDataSource):
    def __init__(self):
        self.connection = ConnectDataSource().connect_mysql()
        self.datasrc = ConnectDataSource().read_database_conf()
        # 数据文件中的客户id列表
        self.customer_ids = self.datasrc["customer_ids"]

    def mysql_add_data(self):
        pass

    def mysql_query_data(self):
        with self.connection.cursor() as cursor:
            # 客户id列表拼接成（id1,id2,id3）格式
            _customer_ids_opted = ", ".join(self.customer_ids)
            # 拼接sql语句
            sql = f'select * from customer where customer_id in ({_customer_ids_opted})'
            print(sql)
            # sql执行
            cursor.execute(sql)
            # 返回所有的查询结果
            result = cursor.fetchall()
            return result

    def mysql_delete_data(self):
        # 删除操作
        with self.connection.cursor() as cursor:
            # 客户id列表拼接成（id1,id2,id3）格式
            _customer_ids_opted = ", ".join(self.customer_ids)
            # 拼接sql
            sql = f'delete from customer where customer_id in ({_customer_ids_opted})'
            print(sql)
            # sql执行
            cursor.execute(sql)
            #  提交
            self.connection.commit()


